import storage from 'store'
import { login, getInfo, getInfoBySaas } from '@/api/login'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { welcome } from '@/utils/util'

const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {},
    loginType: 'tab1'
  },

  mutations: {
    SET_LOGINTYPE: (state, type) => {
      state.loginType = type
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    }
  },

  actions: {
    // 登录
    Login ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          if (response.code === 0) {
            storage.set(ACCESS_TOKEN, response.data.access_token, 7 * 24 * 60 * 60 * 1000)
            commit('SET_TOKEN', response.data.access_token)
            storage.set('isSaas', response.data.is_saas)
            resolve(response)
          } else {
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo ({ commit, state }) {
      return new Promise((resolve, reject) => {
        (storage.get('isSaas') && storage.get('isSaasStore') ? getInfoBySaas() : getInfo()).then(response => {
          const result = response.data

          if (result.role && result.role.permissions.length > 0) {
            const role = result.role
            role.permissions = result.role.permissions
            role.permissions.map(per => {
              if (per.actionEntitySet != null && per.actionEntitySet.length > 0) {
                const action = per.actionEntitySet.map(action => { return action.action })
                per.actionList = action
              }
            })
            role.permissionList = role.permissions.map(permission => { return permission.permissionId })
            commit('SET_ROLES', result.role)
            commit('SET_INFO', result)
            storage.set('isSaas', result.is_saas)
            storage.set('mdId', result.md_id)
            storage.set('isPlatform', result.is_platform)
            storage.set('isSupplier', result.is_supplier)
            storage.set('isDandianpu', result.is_dandianpu)
            if (result.store_name) {
              storage.set('storeName', result.store_name)
            }
            if (result.store_id) {
              storage.set('storeId', result.store_id)
            }
          } else {
            reject(new Error('getInfo: roles must be a non-null array !'))
          }

          commit('SET_NAME', { name: result.name, welcome: welcome() })
          commit('SET_AVATAR', result.avatar)

          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout ({ commit, state }) {
      return new Promise((resolve) => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        commit('SET_INFO', {})
        storage.remove('isSaas')
        storage.remove('isSaasStore')
        storage.remove('isSaasMd')
        storage.remove('mdId')
        storage.remove('storeId')
        storage.remove('isPlatform')
        storage.remove('isSaasSupplier')
        storage.remove('isSupplier')
        storage.remove('currentStoreUsername')
        storage.remove(ACCESS_TOKEN)
        resolve()
      })
    }

  }
}

export default user
